﻿using System.Net;

namespace RStein.Posterous.API
{
    public class PosterousApplication  : IPosterousApplication
    {
        #region private fields
        private IBitLyService m_bitLyService;
        private static PosterousApplication _current = new PosterousApplication();
        private IRawRequestResponsePublisher m_rawRequestResponsePublisher;
        #endregion private fields
        #region constructors

        private PosterousApplication()
        {
            ApplicationName = GlobalConstants.DEFAULT_APPLICATION_NAME;
            ApplicationUrl = GlobalConstants.DEFAULT_APPLICATION_URL;            
        }

        #if SILVERLIGHT
        static PosterousApplication()
        {
            WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);

        }
        #endif 
        #endregion constructors
        
        #region Implementation of IPosterousApplication

        public IRawRequestResponsePublisher RawRequestResponsePublisher
        {
            get
            {
                m_rawRequestResponsePublisher = m_rawRequestResponsePublisher ?? new MainRequestResponsePublisher(this);
                return m_rawRequestResponsePublisher;
            }
            
        }

        public string ApplicationName
        {
            get
            {
                return PosterousRequestBuilder.ApplicationName;
            }
            set
            {
                PosterousRequestBuilder.ApplicationName = value;
            }
        }

        public string ApplicationUrl
        {
            get
            {
                return PosterousRequestBuilder.ApplicationUrl;
            }
            set
            {
                PosterousRequestBuilder.ApplicationUrl = value;
            }
        }

        public IPosterousAccount GetPosterousAccount(string userName, string password)
        {
            return new PosterousAccount(this, userName, password);
        }

        public ITwitterAccount GetTwitterAccount(string userName, string password)
        {
            return new TwitterAccount(this, userName, password);
        }

        public IBitLyService BitLyService
        {
            get
            {
                m_bitLyService = m_bitLyService ?? new BitLyService(this);
                return m_bitLyService;
            }
            
        }
        public static IPosterousApplication Current
        {
            get
            {
                return _current;
                
            }
            
        }
        #endregion

        #region Implementation of IExtensionInterface

        public T GetInterface<T>() where T : class
        {
            if(typeof(T) == typeof(IRawRequestResponsePublisher))
            {
                return RawRequestResponsePublisher as T;
            }
            var result = this as T;
            return result;
        }

        #endregion
    }
}